//
//  TableViewStyle.swift
//  Alamofire
//
//  Created by Fanxx on 2019/7/17.
//

import UIKit

extension UCSetting where TargetType: UITableView {
    ///委托
    public static func delegate(_ delegate: UITableViewDelegate?) -> UCSetting<TargetType> {
        return .init(action: { (target) in
            target.delegate = delegate
        })
    }
    ///数据源
    public static func source(_ dataSource: UITableViewDataSource?) -> UCSetting<TargetType> {
        return .init(action: { (target) in
            target.dataSource = dataSource
        })
    }
    ///prefetchDataSource
    public static func source(prefetch dataSource: UITableViewDataSourcePrefetching?) -> UCSetting<TargetType> {
        return .init(action: { (target) in
            target.prefetchDataSource = dataSource
        })
    }
    ///行高
    public static func row(height:CGFloat) -> UCSetting<TargetType> {
        return .init(action: { (target) in
            target.rowHeight = height
            target.estimatedRowHeight = height
        })
    }
    ///自动行高
    public static func row(estimated height:CGFloat) -> UCSetting<TargetType> {
        return .init(action: { (target) in
            target.estimatedRowHeight = height
            target.rowHeight = UITableView.automaticDimension
        })
    }
    ///Section头部高
    public static func sectionHeader(height:CGFloat) -> UCSetting<TargetType> {
        return .init(action: { (target) in
            target.sectionHeaderHeight = height
            target.estimatedSectionHeaderHeight = height
        })
    }
    ///Section底部高
    public static func sectionFooter(height:CGFloat) -> UCSetting<TargetType> {
        return .init(action: { (target) in
            target.sectionFooterHeight = height
            target.estimatedSectionFooterHeight = height
        })
    }
    ///分隔线样式
    public static func separator(_ config:UITableViewCell.SeparatorStyle, color:UIColor? = nil, insets: UIEdgeInsets? = nil) -> UCSetting<TargetType> {
        return .init(action: { (target) in
            target.separatorStyle = config
            if let s = color { target.separatorColor = s }
            if let s = insets { target.separatorInset = s }
        })
    }
    ///选择
    public static func selection(_ allows:Bool = true) -> UCSetting<TargetType> {
        return .init(action: { (target) in
            target.allowsSelection = allows
        })
    }
    ///选择期间编辑
    public static func selection(edit allows:Bool) -> UCSetting<TargetType> {
        return .init(action: { (target) in
            target.allowsSelectionDuringEditing = allows
        })
    }
    ///选择
    public static func selection(multiple allows:Bool) -> UCSetting<TargetType> {
        return .init(action: { (target) in
            target.allowsMultipleSelection = allows
        })
    }
    ///选择期间编辑
    public static func multipleSelection(edit allows:Bool) -> UCSetting<TargetType> {
        return .init(action: { (target) in
            target.allowsMultipleSelectionDuringEditing = allows
        })
    }
    ///头部
    public static func header(_ view: UIView) -> UCSetting<TargetType> {
        return .init(action: { (target) in
            target.tableHeaderView = view
        })
    }
    ///底部
    public static func footer(_ view: UIView) -> UCSetting<TargetType> {
        return .init(action: { (target) in
            target.tableFooterView = view
        })
    }
    ///将底部置为空View，可清除多余的分隔线
    public static var emptyFooter: UCSetting<TargetType> {
        return .init(action: { (target) in
            target.tableFooterView = UIView()
        })
    }
    ///注册Cell缓存类型
    public static func cell(_ cls: AnyClass, identifier: String = UCCellDefaultIdentifier) -> UCSetting<TargetType> {
        return .init(action: { (target) in
            target.register(cls, forCellReuseIdentifier: identifier)
        })
    }
    ///注册HeaderFooter缓存类型
    public static func headerFooter(_ cls: AnyClass, identifier: String = UCCellDefaultIdentifier) -> UCSetting<TargetType> {
        return .init(action: { (target) in
            target.register(cls, forHeaderFooterViewReuseIdentifier: identifier)
        })
    }
    ///isPrefetchingEnabled
    @available(iOS 15.0, *)
    public static func prefetch(_ enable:Bool = true) -> UCSetting<TargetType> {
        return .init(action: { (target) in
            target.isPrefetchingEnabled = enable
        })
    }
    ///dragDelegate
    @available(iOS 11.0, *)
    public static func delegate(drag delegate: UITableViewDragDelegate?) -> UCSetting<TargetType> {
        return .init(action: { (target) in
            target.dragDelegate = delegate
        })
    }
    ///dropDelegate
    @available(iOS 11.0, *)
    public static func delegate(drop delegate: UITableViewDropDelegate?) -> UCSetting<TargetType> {
        return .init(action: { (target) in
            target.dropDelegate = delegate
        })
    }
    ///fillerRowHeight
    @available(iOS 15.0, *)
    public static func row(filler height:CGFloat) -> UCSetting<TargetType> {
        return .init(action: { (target) in
            target.fillerRowHeight = height
        })
    }
    ///sectionHeaderTopPadding
    @available(iOS 15.0, *)
    public static func header(top padding:CGFloat) -> UCSetting<TargetType> {
        return .init(action: { (target) in
            target.sectionHeaderTopPadding = padding
        })
    }
    ///separatorInsetReference
    @available(iOS 11.0, *)
    public static func separator(insetRef reference:UITableView.SeparatorInsetReference) -> UCSetting<TargetType> {
        return .init(action: { (target) in
            target.separatorInsetReference = reference
        })
    }
    /// 背景视图
    public static func bg(_ view: UIView?) -> UCSetting<TargetType> {
        return .init(action: { (target) in
            target.backgroundView = view
        })
    }
    ///performBatchUpdates(_ updates: (() -> Void)?, completion: ((Bool) -> Void)? = nil)
    @available(iOS 11.0, *)
    public static func batch(updates: (() -> Void)?, completion: ((Bool) -> Void)? = nil) -> UCSetting<TargetType> {
        return .init(action: { (target) in
            target.performBatchUpdates(updates, completion: completion)
        })
    }
    /// sectionIndexMinimumDisplayRowCount
    public static func min(indexRow count: Int) -> UCSetting<TargetType> {
        return .init(action: { (target) in
            target.sectionIndexMinimumDisplayRowCount = count
        })
    }
    /// sectionIndexColor
    public static func color(index color: UIColor?) -> UCSetting<TargetType> {
        return .init(action: { (target) in
            target.sectionIndexColor = color
        })
    }
    /// sectionIndexBackgroundColor
    public static func bg(index color: UIColor?) -> UCSetting<TargetType> {
        return .init(action: { (target) in
            target.sectionIndexBackgroundColor = color
        })
    }
    /// sectionIndexTrackingBackgroundColor
    public static func tracking(index color: UIColor?) -> UCSetting<TargetType> {
        return .init(action: { (target) in
            target.sectionIndexTrackingBackgroundColor = color
        })
    }
    /// separatorEffect
    public static func separator(effect: UIVisualEffect?) -> UCSetting<TargetType> {
        return .init(action: { (target) in
            target.separatorEffect = effect
        })
    }
    /// cellLayoutMarginsFollowReadableWidth
    public static func layoutFollowReadable(_ follow: Bool = true) -> UCSetting<TargetType> {
        return .init(action: { (target) in
            target.cellLayoutMarginsFollowReadableWidth = follow
        })
    }
    /// insetsContentViewsToSafeArea
    @available(iOS 11.0, *)
    public static func inset(safe enable: Bool) -> UCSetting<TargetType> {
        return .init(action: { (target) in
            target.insetsContentViewsToSafeArea = enable
        })
    }
    ///remembersLastFocusedIndexPath
    public static func remember(lastFocus enable: Bool) -> UCSetting<TargetType> {
        return .init(action: { (target) in
            target.remembersLastFocusedIndexPath = enable
        })
    }
    ///selectionFollowsFocus
    @available(iOS 14.0, *)
    public static func selection(followsFocus: Bool) -> UCSetting<TargetType> {
        return .init(action: { (target) in
            target.selectionFollowsFocus = followsFocus
        })
    }
    ///allowsFocus
    @available(iOS 15.0, *)
    public static func focus(_ allow: Bool = true) -> UCSetting<TargetType> {
        return .init(action: { (target) in
            target.allowsFocus = allow
        })
    }
    ///allowsFocusDuringEditing
    @available(iOS 15.0, *)
    public static func focus(edit allow: Bool) -> UCSetting<TargetType> {
        return .init(action: { (target) in
            target.allowsFocusDuringEditing = allow
        })
    }
    ///dragInteractionEnabled
    @available(iOS 11.0, *)
    public static func drag(interaction enabled: Bool) -> UCSetting<TargetType> {
        return .init(action: { (target) in
            target.dragInteractionEnabled = enabled
        })
    }
}
